package com.shawn.service;

import android.app.ActivityManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

import com.shawn.db.dao.WatchDogDao;
import com.shawn.mobliesafe.WatchDogActivity;

import java.util.List;

public class WatchDogService extends Service {

    private WatchDogDao watchDogDao;
    private boolean isLock;
    private List<String> list;
    private UnlockCurrentReceiver unlockCurrentReceiver;
    private String unlockcurrentPackagname;
    private ScreenOffReceiver screenOffReceiver;

    public WatchDogService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private class UnlockCurrentReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            unlockcurrentPackagname = intent.getStringExtra("packagename");
        }
    }

    private class ScreenOffReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            unlockcurrentPackagname = null;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();

        //注册解锁的广播接受者
        unlockCurrentReceiver = new UnlockCurrentReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.shawn.mobliesafe.unlock");
        registerReceiver(unlockCurrentReceiver,intentFilter);

        //注册锁屏的广播接受者
        screenOffReceiver = new ScreenOffReceiver();
        IntentFilter screenOffIntentFilter = new IntentFilter();
        screenOffIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(screenOffReceiver,screenOffIntentFilter);


        watchDogDao = new WatchDogDao(this);
        //时时刻刻监听用户打开的程序
        //activity都是存放在任务栈中的,一个应用只有一个任务栈
        final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        isLock = true;
        new Thread(){
            @Override
            public void run() {

                //当数据库变化的时候重新更新内存中的数据,当数据库变化的时候通知内容观察者数据库变化了,然后在内容观察者中去更新最新的数据
                Uri uri = Uri.parse("content://com.shawn.mobliesafe75.lock.changed");
                getContentResolver().registerContentObserver(uri, true, new ContentObserver(null) {
                    @Override
                    public void onChange(boolean selfChange) {
                        super.onChange(selfChange);
                        list = watchDogDao.querAllLockApp();
                    }
                });
                list = watchDogDao.querAllLockApp();
                while(isLock) {
                    //监听操作
                    //监听用户打开了哪些任务栈,打开哪些应用
                    //获取正在运行的任务栈,如果任务栈运行,就表示应用打开过
                    //maxNum : 获取正在运行的任务栈的个数
                    //现在打开的应用的任务栈,永远在第一个,而之前(点击home最小化,没有退出)的应用的任务栈会依次往后推
                    List<ActivityManager.RunningTaskInfo> runningTasks = am.getRunningTasks(1);
                    for (ActivityManager.RunningTaskInfo runningTaskInfo : runningTasks){
                        ComponentName baseActivity = runningTaskInfo.baseActivity;
                        String packageName = baseActivity.getPackageName();
                        boolean isContains = list.contains(packageName);
                        if(isContains) {
                            if(!packageName.equals(unlockcurrentPackagname)) {
                                Intent intent = new Intent(WatchDogService.this,WatchDogActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.putExtra("packageName",packageName);
                                startActivity(intent);
                            }
                        }
                    }
                    SystemClock.sleep(300);
                }

            }
        }.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //当服务关闭的禁止监听用户打开的应用程序
        isLock = false;
        //注销解锁的广播接受者
        if (unlockCurrentReceiver != null) {
            unregisterReceiver(unlockCurrentReceiver);
            unlockCurrentReceiver = null;
        }
        //注销锁屏的广播接受者
        if (screenOffReceiver != null) {
            unregisterReceiver(screenOffReceiver);
            screenOffReceiver = null;
        }
    }
}
